package com.lagou.edu.utils;

import com.lagou.edu.annotations.Autowired;
import com.lagou.edu.annotations.Component;

import java.sql.SQLException;

/**
 * 事务管理者
 */
@Component("transactionManger")
public class TransactionManger {

    //创建对象交给Ioc容器
    @Autowired
    private ConnectionUtils connectionUtils;

    /**
     * 关闭数据库连接自动提交事务
     *
     * @throws SQLException
     */
    public void closeAutoCommit() throws SQLException {
        connectionUtils.getConnection().setAutoCommit(false);
    }

    /**
     * 提交事务
     *
     * @throws SQLException
     */
    public void commit() throws SQLException {
        connectionUtils.getConnection().commit();
    }

    /**
     * 回滚事务
     *
     * @throws SQLException
     */
    public void rollback() throws SQLException {
        connectionUtils.getConnection().rollback();
    }
}
